package com.example.todolist.controller;

import com.example.todolist.entity.Teaching;
import com.example.todolist.service.TeachingService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teaching")
@Api(tags = "教学信息API")
public class TeachingController {
    @Autowired
    private TeachingService teachingService;

    @ApiOperation("查询所有教学信息")
    @GetMapping("/getAll")
    public List<Teaching> getAll() {
        return teachingService.getAll();
    }

    @ApiOperation("根据user_id、device_id、type更新question字段")
    @PostMapping("/updateQuestion")
    public boolean updateQuestion(@RequestBody Map<String, Object> req) {
        Integer userId = (Integer) req.get("user_id");
        Integer deviceId = (Integer) req.get("device_id");
        String type = (String) req.get("type");
        String question = (String) req.get("question");
        return teachingService.updateQuestion(userId, deviceId, type, question);
    }

    @ApiOperation("根据user_id、device_id、type更新answer字段")
    @PostMapping("/updateAnswer")
    public boolean updateAnswer(@RequestBody Map<String, Object> req) {
        Integer userId = (Integer) req.get("user_id");
        Integer deviceId = (Integer) req.get("device_id");
        String type = (String) req.get("type");
        String answer = (String) req.get("answer");
        return teachingService.updateAnswer(userId, deviceId, type, answer);
    }

    @ApiOperation("根据user_id、device_id、type查询answer字段")
    @GetMapping("/getAnswerByParams/{userId}/{deviceId}/{type}")
    public Teaching getAnswerByParams(@PathVariable Integer userId, @PathVariable Integer deviceId, @PathVariable String type) {
        return teachingService.getAnswer(userId, deviceId, type);
    }

    @ApiOperation("查询所有教学信息 (通过getAnswer接口)")
    @GetMapping("/getAnswer")
    public List<Teaching> getAnswer() {
        return teachingService.getAll();
    }

    @ApiOperation("清空所有教学信息")
    @PostMapping("/clearAll")
    public Map<String, Object> clearAll() {
        boolean success = teachingService.clearAllTeachingData();
        Map<String, Object> result = new java.util.HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("msg", "成功清空所有教学信息");
        } else {
            result.put("code", 500);
            result.put("msg", "清空教学信息失败");
        }
        return result;
    }
} 